  import {Component, EventEmitter, Input, Output} from "@angular/core";
import {Router} from "@angular/router";
import {NzNotificationService} from "ng-zorro-antd";
import {HttpServcie} from "../../../service/http.service";
import {TrainingPlanBean} from "../bean/trainingPlan.bean";
import {FormBuilder,FormGroup,Validators} from "@angular/forms";
import {Config} from "../../../service/config";
import * as moment from "moment";

@Component({
  selector: "trainingPlan-update-page",
  templateUrl: "trainingPlan-update.html",
  styleUrls: ["trainingPlan-update.scss"]
})

export class TrainingPlanUpdate {
  constructor(
    private httpService: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private fb: FormBuilder
  ) { }
  trainingPlanBean : TrainingPlanBean = new TrainingPlanBean();
  @Input() inValue: any = [];
  @Output() outValue = new EventEmitter();
  validateForm: FormGroup;
  config = Config;

  ngOnInit(){
    this.validateForm = this.fb.group({
      planName: [null, [Validators.required]], //计划名称
      planType: [null, [Validators.required]], //计划类型
      planContent: [null, [Validators.required]], //计划描述
      leadUnitName: [null, [Validators.required]], //牵头单位
      userName: [null, [Validators.required]], //负责人
      startDate: [null, [Validators.required]], //开始时间
      endDate: [null, [Validators.required]] //结束时间
    });

    if (this.inValue) {
      this.trainingPlanBean = Object.assign({}, this.inValue);
    }
  }

  submitForm()  {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
    }
    //处理是否提交
    for (const i in this.validateForm.controls) {
      if (!this.validateForm.controls[i].valid) {
        return;
      }
    }
    this.trainingPlanBean.startDate = moment(this.trainingPlanBean.startDate).format("YYYYMMDD");
    this.trainingPlanBean.endDate = moment(this.trainingPlanBean.endDate).format("YYYYMMDD");
    this.httpService.updateTrainingPlan(this.trainingPlanBean).subscribe(data => {
      if (data["status"] != "200") {
        this._notification.create("error", "错误提示", "修改失败！");
      }
      if (data["status"] === "200") {
        this._notification.create("success", "", "修改成功！");
      }
      this.outValue.emit(true);
    });
  }

  getFormControl(name) {
    return this.validateForm.controls[name];
  }

  quit() {
    this.outValue.emit(true);
  }

  // 查询面板
  panels = {
    active: true,
    name: "查询",
    disabled: false,
    childPanel: [
      {
        active: false,
        name: "查询"
      }
    ]
  };

}
